﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace WqLunTan.Models.DataBase
{
    /// <summary>
    /// 微信推送消息数据
    /// </summary>
    [Table("wechat_data")]
    public class wechat_data
    {
        /// <summary>
        /// 消息的唯一标识符
        /// </summary>
        [Key]
        public long MsgID { get; set; }

        /// <summary>
        /// 开发人员账号
        /// </summary>
        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string ToUserName { get; set; } = string.Empty;

        /// <summary>
        /// openid, 发消息的用户 id
        /// </summary>
        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string FromUserName { get; set; } = string.Empty;

        /// <summary>
        /// Unix时间戳, 1970开始
        /// </summary>
        public long CreateTime { get; set; }

        /// <summary>
        /// UTC时间
        /// </summary>
        public DateTimeOffset CreateTimeUtc { get; set; }

        /// <summary>
        /// 消息类型
        /// </summary>
        public MsgType MsgType { get; set; }

        /// <summary>
        /// 文本消息字段
        /// </summary>
        [MaxLength(1024)]
        [Required(AllowEmptyStrings = true)]
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 图片链接（由系统生成）
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string PicUrl { get; set; } = string.Empty;

        /// <summary>
        /// 图片,语音,视频 消息媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string MediaId { get; set; } = string.Empty;

        /// <summary>
        /// 语音格式，如amr，speex等
        /// </summary>
        [MaxLength(50)]
        public string Format { get; set; } = string.Empty;

        /// <summary>
        /// 语音识别结果
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string Recognition { get; set; } = string.Empty;

        /// <summary>
        /// 视频消息缩略图的媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string ThumbMediaId { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Location_X { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Location_Y { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Scale { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Label { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Title { get; set; } = string.Empty;

        [MaxLength(50)]
        [Required(AllowEmptyStrings = true)]
        public string Description { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string Url { get; set; } = string.Empty;

        /// <summary>
        /// 事件类型, subscribe 订阅 unsubscribe 取消订阅 SCAN 已关注再次扫码
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string Event { get; set; }

        /// <summary>
        /// 事件KEY值 qrscene_为前缀，后面为二维码的参数值
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string EventKey { get; set; }

        /// <summary>
        /// 二维码的ticket，可用来换取二维码图片
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string Ticket { get; set; }
    }

    /// <summary>
    /// 消息类型
    /// </summary>
    public enum MsgType
    {
        text = 0,
        image = 1,
        voice = 2,
        video = 3,
        shortvideo = 4,
        location = 5,
        link = 6,
        @event = 7
    }

    /// <summary>
    /// WeChatData扩展类
    /// </summary>
    public static class WeChatDataExtensions
    {
        /// <summary>
        /// 去除实体类中string字段的null值, 改成空字符串
        /// </summary>
        /// <param name="we"></param>
        /// <returns></returns>
        public static wechat_data NotNullStrings(this wechat_data we)
        {
            we.ToUserName = we.ToUserName ?? string.Empty;
            we.FromUserName = we.FromUserName ?? string.Empty;
            we.Content = we.Content ?? string.Empty;
            we.PicUrl = we.PicUrl ?? string.Empty;
            we.MediaId = we.MediaId ?? string.Empty;
            we.Format = we.Format ?? string.Empty;
            we.Recognition = we.Recognition ?? string.Empty;
            we.ThumbMediaId = we.ThumbMediaId ?? string.Empty;
            we.Location_X = we.Location_X ?? string.Empty;
            we.Location_Y = we.Location_Y ?? string.Empty;
            we.Scale = we.Scale ?? string.Empty;
            we.Label = we.Label ?? string.Empty;
            we.Title = we.Title ?? string.Empty;
            we.Description = we.Description ?? string.Empty;
            we.Url = we.Url ?? string.Empty;
            we.Event = we.Event ?? string.Empty;
            we.EventKey = we.EventKey ?? string.Empty;
            we.Ticket = we.Ticket ?? string.Empty;
            return we;
        }
    }
}
